package com.lib.common.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.util.Log;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import java.io.File;
import java.util.List;

/**
 * 与应用相关的一些实用方法
 */
public class AppUtil {
	private static boolean isRelease = false;


	/**
	 * 用 System.out.println 打印调试信息
	 * ( 若调试开关关闭，则不打印 )
	 */
	public static void print(String x) {
		if (null == x || !isRelease) {
			return;
		}

		System.out.println(x);
	}

	/**
	 * 往屏幕输出普通调试信息
	 * ( 若调试开关关闭，则不打印 )
	 * @param tag
	 * @param msg
	 */
	public static void verbose(String tag, String msg) {
		if (null == msg || !isRelease) {
			return;
		}

		Log.v(null == tag ? "^_^" : tag, msg);
	}

	/**
	 * 往屏幕输出普通调试信息
	 * 
	 * ( 若调试开关关闭，则不打印 )
	 * @param msg
	 */
	public static void verbose(String msg) {
		if (null == msg || !isRelease) {
			return;
		}

		verbose(null, msg);
	}

	/**
	 * 用红字输出出错信息
	 * 
	 * ( 若调试开关关闭，则不打印 )
	 * @param msg
	 */
	public static void error(Object obj, String msg) {
		if (null == msg || !isRelease) {
			return;
		}

		String tag = "^_^";
		if (null != obj && obj instanceof String) {
			tag = (String) obj;
		} else if (null != obj) {
			tag = getClassName(obj);
		}

		Log.e(tag, msg);
	}

	/**
	 * 用红字输出出错信息
	 * 
	 * ( 若调试开关关闭，则不打印 )
	 * @param msg
	 */
	public static void error(String msg) {
		if (null == msg || !isRelease) {
			return;
		}

		error(null, msg);
	}

	/**
	 * 打印出错信息的 StackTrace 信息
	 * 
	 * ( 若调试开关关闭，则不打印 )
	 */
	public static void print(Throwable e) {
		if (null == e || !isRelease) {
			return;
		}

		if (e instanceof Throwable) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取一个对象的类名
	 * @return String
	 */
	public static String getClassName(Object x) {
		if (null != x) {
			try {
				String className;
				if (x instanceof Class<?>) {
					className = ((Class<?>) x).getName();
				} else {
					className = x.getClass().getName();
				}

				String[] classNames = className.split("\\.");
				return classNames[classNames.length - 1];
			} catch (Exception e) {
				AppUtil.print(e);
			}
		}

		return "";
	}

	/**
	 * 获取一个文件的大小
	 * 
	 * @param filePath
	 * @return String
	 */
	public static String getFileSize(String filePath) {
		if (null == filePath || filePath.length() < 1) {
			return "";
		}

		File file = new File(filePath);
		if (!file.exists()) {
			return "";
		}

		return getStringSize(file.length());
	}

	/**
	 * 获取字符串形式的容量
	 * 
	 * @param fileSize
	 * @return String
	 */
	public static String getStringSize(long fileSize) {
		if (fileSize < 1) {
			return "0 Byte";
		}

		float finallySize = 0;
		String unit;

		if (fileSize < 1024) {
			finallySize = fileSize;
			unit = "Bytes";
		} else if (fileSize < 1024 * 1024) {
			finallySize = (float) (Math.ceil(100 * (fileSize / 1024.0f)) / 100);
			unit = "KB";
		} else if (fileSize < 1024 * 1024 * 1024) {
			finallySize = (float) (Math.ceil(100 * (fileSize / 1024.0f / 1024.0f)) / 100);
			unit = "MB";
		} else {
			finallySize = (float) (Math.ceil(100 * (fileSize / 1024.0f / 1024.0f / 1024.0f)) / 100);
			unit = "GB";
		}

		return (finallySize + " " + unit);
	}

	public static void hiddenKeyBoard(Context context, EditText edit){
		InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
		inputMethodManager.hideSoftInputFromWindow(edit.getWindowToken(), 0);
	}

    public static int px2dp(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static int dp2px(Context context, float dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

	/** 获取版本号 */
	public static int getVersionCode(Context context) {
		PackageManager manager = context.getPackageManager();
		int code = 0;
		try {
			PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
			code = info.versionCode;
		} catch (PackageManager.NameNotFoundException e) {
			e.printStackTrace();
		}
		return code;
	}

	/**
	 * 检测程序是否安装
	 * @param packageName
	 * @return
	 */
	public static boolean isInstalled(Context context, String packageName) {
		PackageManager manager = context.getPackageManager();
		List<PackageInfo> installedPackages = manager.getInstalledPackages(0);
		if (installedPackages != null) {
			for (PackageInfo info : installedPackages) {
				if (info.packageName.equals(packageName))
					return true;
			}
		}
		return false;
	}

	// 获取指定应用的签名
	public static Signature[] getRawSignature(Context paramContext, String paramString) {
		if ((paramString == null) || (paramString.length() == 0)) {
//			errout("获取签名失败，包名为 null");
			return null;
		}
		PackageManager localPackageManager = paramContext.getPackageManager();
		PackageInfo localPackageInfo;
		try {
			localPackageInfo = localPackageManager.getPackageInfo(paramString, PackageManager.GET_SIGNATURES);
			if (localPackageInfo == null) {
//				errout("信息为 null, 包名 = " + paramString);
				return null;
			}
		} catch (PackageManager.NameNotFoundException localNameNotFoundException) {
//			errout("包名没有找到...");
			return null;
		}
		return localPackageInfo.signatures;
	}
}
